<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">
          <img src="https://likede2-admin.itheima.net/img/logo.595745bd.png" alt>
        </h3>
      </div>

      <el-form-item prop="loginName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input v-model="loginForm.loginName" placeholder="请输入用户名" name="username" type="text" />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          ref="password"
          v-model="loginForm.password"
          :type="pwdType ? 'password' : 'text'"
          placeholder="请输入密码"
          name="password"
        />
        <span class="show-pwd" @click="changeType">
          <svg-icon :icon-class="pwdType ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <div class="test">
        <el-form-item prop="code">
          <span class="svg-container">
            <span class="el-icon-a-ziyuan109" />
          </span>
          <el-input v-model="loginForm.code" placeholder="请输入验证码" name="usercode" type="text" />
        </el-form-item>
        <img :src="testImg" @click="getTestCode">
      </div>

      <el-button
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom:30px;"
        @click.native.prevent="handleLogin"
      >登录</el-button>
    </el-form>
  </div>
</template>

<script>
import { getImageCode } from '@/api/user'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        clientToken: '',
        code: '',
        loginType: 0
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入正确的手机号' },
          { pattern: /^\w{5,15}$/, trigger: 'blur', message: '请输入正确的手机号' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入正确密码' },
          { min: 5, max: 15, trigger: 'blur', message: '请输入正确密码' }
        ],
        code: [
          { required: true, trigger: 'blur', message: '请输入验证码' }
        ]
      },
      loading: false,
      pwdType: true,
      testImg: ''
    }
  },
  created() {
    // 拿到验证码图片
    this.getTestCode()
  },
  methods: {
    changeType() {
      this.pwdType = !this.pwdType
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    async getTestCode() {
      const num = Math.floor(Math.random() * 100)
      this.loginForm.clientToken = num
      // 获取验证码图片
      const res = await getImageCode(num)
      // console.log(res)
      this.testImg = 'data:image/png;base64,' + btoa(new Uint8Array(res).reduce((data, byte) => data + String.fromCharCode(byte), ''))
    },
    async handleLogin() {
      this.loading = true
      try {
        await this.$refs.loginForm.validate()
        console.log(this.loginForm)
        await this.$store.dispatch('user/login', this.loginForm)
      } catch (err) {
        throw Error('验证未通过')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #000;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  .el-form {
    background-color: #fff;
    padding-top: 0 !important;
    height: 388px;
    border-radius: 10px;
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
    .el-button {
      margin-top: 20px;
    }
    .el-form-item {
      border: 1px solid rgba(0, 0, 0, 0.1);
      background: rgba(255, 255, 255, 0.5);
      border-radius: 5px;
      color: #454545;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  // overflow: hidden;
  background: url('https://likede2-admin.itheima.net/img/background.be4fae7d.png')
    no-repeat 0 0 / cover;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    // overflow: hidden;
    .test {
      height: 50px;
      display: flex;
      .el-form-item {
        flex: 1;
        height: 100%;
      }
      img {
        width: 130px;
      }
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    .title {
      margin: 0px auto 40px auto;
      width: 96px;
      height: 96px;
      border-radius: 50%;
      margin-top: -50px;
      img {
        width: 100%;
      }
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
